*Note that including group quarters' residents, because it seems like they could be 
*the children of staff.

clear

do "usa_00065.do"

clear
use "schattend_1940.dta"

merge m:1 stateicp countyicp using "icpfips.dta"

save "schattend_1940_ctyfip.dta", replace

keep if hasnormalorasylum==1

gen age1617 = age>=16 & age<=17 & higrade~=99

egen tagcty = tag(cty_fips)

gen hsgrad = higrade>=15
replace hsgrad = . if higrade==99

gen attend = school==2
replace attend = . if higrade==99

local vars age1617
local other attend

foreach x of local vars{
bysort cty_fips: egen tot`x' = total(`x')
foreach y of local other{
gen `x'`y' = `x'==1 & `y'==1
bysort cty_fips: egen tot`x'`y' = total(`x'`y')
gen frac`y'`x' = tot`x'`y'/tot`x'
}
}


gen hsgradattend1617 = hsgrad==1 & attend==1 & age1617==1
gen nonhsgradattend1617 = hsgrad==0 & attend==1 & age1617==1
gen nonhsgrad1617 = hsgrad==0 & age1617==1
gen hsgrad1617 = hsgrad==1 & age1617==1

bysort cty_fips: egen totalschhsgrad1617 = total(hsgradattend1617) 

bysort cty_fips: egen totalschnonhsgrad1617 = total(nonhsgradattend1617)

bysort cty_fips: egen totalhsgrad1617 = total(hsgrad1617) 

bysort cty_fips: egen totalnonhsgrad1617 = total(nonhsgrad1617)

gen fracattend1617hsgrad = totalschhsgrad1617/totage1617
gen fracattend1617nonhsgrad = totalschnonhsgrad1617/totage1617


#delimit ;
local vars fracattendage1617 fracattend1617hsgrad fracattend1617nonhsgrad ;
#delimit cr
foreach x of local vars{
reghdfe  `x' hasnormalschool if hasnormalorasylum==1 & tagcty==1, absorb(stateicp) cluster(stateicp)
outreg2 using fullctschattend.xls, append excel dec(3)

#delimit ;
tabout hasnormalschool if hasnormalorasylum==1 & tagcty==1 using sumstats_1940.xls,
append c(mean `x' sd `x') npos(row) f(2)  layout(row) sum show(none); 
#delimit cr

}

save "schattend_1940_ctyfip_NA.dta", replace
